package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.UserRole;
import com.example.springboot.service.UserRoleService;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author linyue
 * @since 2024-04-25
 */
@RestController
@RequestMapping("/userRole")
@Api(value = "用户角色管理", tags = "用户角色管理")
public class UserRoleController {
    @Resource
    private UserRoleService userRoleService;

    @PostMapping("/add")
    @ApiOperation(value = "添加权限")
    public Result add(@RequestBody UserRole userRole) {
        userRoleService.save(userRole);
        return Result.success();
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除权限", notes = "根据id删除权限")
    public Result deleteById(@PathVariable Integer id) {
        userRoleService.removeById(id);
        return Result.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改权限", notes = "修改权限")
    public Result updateById(@RequestBody UserRole userRole) {
        userRoleService.updateById(userRole);
        return Result.success();
    }

    @GetMapping("/findByUser")
    @ApiOperation(value = "根据用户名查询权限", notes = "根据用户名查询权限")
    public Result selectByUser(@RequestParam String username) {
        return userRoleService.selectByUser(username);
    }

    @GetMapping("/selectAll")
    @ApiOperation(value = "查询所有权限", notes = "查询所有权限")
    public Result selectAll(@RequestParam Integer pageNum,
                            @RequestParam Integer pageSize) {
        PageMethod.startPage(pageNum, pageSize);
        List<UserRole> list = userRoleService.list();
        PageInfo<UserRole> roleList = new PageInfo<>(list);
        return Result.success(roleList);
    }


}
